<?php
define("KEY_PRIVATE", "
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCpvdExvx5zybwFboU1NSjuIghiuXNQSNiJ27G8iETbJdhtibKv
TlbyzBVhq0brhnG16MG29pf8055zwODYKpWC+hPVJTflzhcYEVEqRpstq9q2BHe7
5ErRgjYIQiNGZJ11czQUj82TgDP3xMKntpchel5J3XbYfGjMzG3nARoOpwIDAQAB
AoGAa42A3hqReyCaaH6tHRfABZO+6H9Fl9twiXmRVzqpFosWZeHYPMhQw6uY6LgU
Sex4ZKFFDV+W0nZsckAGuDHTZY+j9mrmz+DJLknBFSqIr29T4bPRDEkCKgQSALAE
Xu3XRDeichbQPbg1D79Stisg74Exu4w96cRiKUb4Gv4UeUECQQDf3rqH1Cf/ILZT
TWfVGknOYAITWXWrcgOGfetZElYlIoDdV0hMK9+JZFRNTODUcbftuEm0Fzssof5J
CoDPYkzRAkEAwhpWA3vQ/u4zXWED4QnGFsqnd1V8WUiKrKYxfQowNndUhEOXeSoz
cx0RoSTJZkzzEsAJRftGpRXwuJHbgmsh9wJASM6lF3u2LlEzOtBXzoSoMEglJKnZ
jRKdduYI3oUKIzWfd1zllHBIWOSaTjGMhUseqF9hCQLtKczdaF/UGJ49oQJAGLEi
+gR5PMTukdVymeEVavbSwPKGBBGfWoE10HNIWNVrUfwLO+Wrb9xlzrsQR8xIK710
MIvbm8qjuccMRpX8VwJBAJ7J8s0A7MBuKu89esWw0Rx6lkxy2cmHqOT4h1yN2HTV
WgygOJ/d+KdmPBoPeLI4cMj7hmH243hZcrN0qF9Q9BE=
-----END RSA PRIVATE KEY-----
");

//解密方法
function decrypt($msg){
    $decrypted = "";

    openssl_private_decrypt(base64_decode($msg),$decrypted,KEY_PRIVATE);//私钥解密
    return $decrypted;
}

function getParamArr($ParamStr){

    if(!$ParamStr){
        $ParamStr = 'z';
    }

    $ParamStr = $_REQUEST[$ParamStr];

    $pattern = "/\[([^=]+)=\{\{(.*?)\}\}\]/";

    preg_match_all($pattern, $ParamStr,$match);

    $Params = array();
    for($i=0;$i<count($match[1]);$i++){
        $Params[$match[1][$i]] = strval($match[2][$i]);
    }

    return $Params;
}
function getPostParamArr($ParamStr){

    if(!$ParamStr){
        $ParamStr = 'z';
    }

    $ParamStr = urldecode($_POST[$ParamStr]);
    $pattern = "/\[([^=]+)=\{\{(.*?)\}\}\]/";

    preg_match_all($pattern, $ParamStr,$match);

    $Params = array();
    for($i=0;$i<count($match[1]);$i++){
        $Params[$match[1][$i]] = strval($match[2][$i]);
    }

    return $Params;
}

//解开参数
function getRsaPostParamArr($ParamStr){

    if(!$ParamStr){
        $ParamStr = 'z';
    }

    $ParamStr = decrypt($_POST[$ParamStr]);

    $pattern = "/\[([^=]+)=\{\{(.*?)\}\}\]/";

    preg_match_all($pattern, $ParamStr,$match);

    $Params = array();
    for($i=0;$i<count($match[1]);$i++){
        $Params[$match[1][$i]] = strval($match[2][$i]);
    }

    return $Params;
}

function getSuccessJson($list,$errorname){
    $JsonArr = array();
    $JsonPrefix = strtolower(CONTROLLER_NAME."_".ACTION_NAME);

    $JsonArr["code"] = 200;
    $JsonArr["result"] = array();
    if($errorname){
        $JsonArr["errorname"] = $errorname;
    }else{
        $JsonArr["errorname"] = "操作失败";
    }
    if(sizeof($list) == 1){
        if($list[0] == null){
            $JsonArr['result']=[];
            return json_encode($JsonArr,JSON_UNESCAPED_UNICODE);
        }else{
            $JsonArr['result']=$list[0];
            return json_encode($JsonArr,JSON_UNESCAPED_UNICODE);
        }
    }
    foreach ($list as $key => $value){
        if(!is_numeric($key)){
            $JsonArr["result"][$key] = $value;
        }
    }

    foreach ($list as $key => $value){
        if(is_numeric($key)){
            $key = $JsonPrefix."_list".(intval($key)+1);
            $JsonArr["result"][$key] = $value;
        }
    }
    return json_encode($JsonArr,JSON_UNESCAPED_UNICODE);
}

function getErrorJson($error,$errorname){
    $JsonArr = array();
    $JsonPrefix = strtolower(CONTROLLER_NAME."_".ACTION_NAME);

    if($error){
        $JsonArr["error"] = $error;
    }else{
        $JsonArr["error"] = "10001";
    }
    if($errorname){
        $JsonArr["errorname"] = $errorname;
    }else{
        $JsonArr["errorname"] = "操作失败";
    }
    return json_encode($JsonArr,JSON_UNESCAPED_UNICODE);
}

//递归获取分级
//递归获取分级
//递归获取分级
function recursion($data,$id,$level){

    if($level == 4){
        return;
    }
    $list = array();
    foreach($data as $v){
        if($v['upper'] == $id){
            
            $v['son'] = recursion($data,$v['lower'],++$level);
            --$level;
            if(empty($v['son'])) {
                unset($v['son']);
            }
            array_push($list, $v);
        }

    }
    return $list;
}

//递归获取上级
function getUpper($data,$id,$level){
    if($level == 4){
        return;
    }
    $list = array();

    foreach($data as $v){
        if($v['lower'] == $id){
            $v['son'] = getUpper($data,$v['upper'],++$level);
             --$level;
            if(empty($v['son'])) {
                unset($v['son']);
            }
            array_push($list, $v);
        }
    }
    return $list;
}

//递归获取下级
function getLower($data,$id){
    $list = array();

    foreach($data as $v){
        if($v['upper'] == $id){
            $v['son'] = getLower($data,$v['lower']);

            if(empty($v['son'])) {
                unset($v['son']);
            }
            array_push($list, $v);
        }
    }
    return $list;
}
